// pages/make_order/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
const clicknum=1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        buyPop:false,
        goodsList:null,
        is_createOrder:false,
        goodsId:0,
        goodnum:0,
        key: 0,
        order_date:'',
        note:''
    },
    gocards(e){
     wx.navigateTo({
       url: '/pages/my_rights/index',
     })
    },
    inputNote(e){
        this.setData({
            note:e.detail.value
        })
    },
    buyPopToggle(){
        this.setData({
            buyPop:!this.data.buyPop
        })
    },
    buyPopToggle1(){
        this.setData({
            buyPop1:!this.data.buyPop1
        })
    },
    goappoint(){
      wx.navigateTo({
        url: '/pages/my_orders/index',
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
       console.log(options)
       this.setData({
        goodsId: options.goodsId,
        goodnum: options.goodnum,
        key: options.key,
      })
      
     
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        
     this.getPaySure()
    },
    getPaySure(){
        var json = {},that=this;
        json.goodsId=that.data.goodsId;
        json.goodnum=that.data.goodnum;
        json.key = that.data.key;
       
        request.requestLoading(
            "/api/order/orderSure",
            json,
            'POST',
          ).then((res) => {
            that.setData({
                goods: res.data,
                order_date:res.data.order_date,
                total_price:res.data.total_price,
                total_fee:res.data.total_fee,
                type: that.data.type ? that.data.type : 'good',
                goodsId: that.data.goodsId,
                goodnum: that.data.goodnum,
                key: that.data.key,
              })
          
           
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    submit()
    {
      var  that = this;
  
      if(this.data.is_createOrder){
        wx.showToast({
          title: '无需重复支付',
          icon:'none'
        })
        setTimeout(function(){
            wx.navigateBack({
                delta:1
            })
        },1500)
        return false
    }
      
      
        
        wx.showLoading({
            title: '正在付款中...',
            mask: true
          })
        var data = {
          uid: wx.getStorageSync('wx_uid'),
          goodsId: this.data.goodsId,
          goodnum: this.data.goodnum,
          key: this.data.key, 
          note: this.data.note ? this.data.note : '无',
        };
        if (clicknum > 1) {
          wx.showToast({
            title: '请勿再次提交订单',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
        request.requestLoading(
            "/api/order/setUpOrder",
            data,
            'POST',
          ).then((res) => {
           if(res.code==1){
            that.setData({
                is_createOrder:true
            })
              if(res.data.is_pay==1){
              
                var wxconfig = res.data.wxconfig;
                // 支付
                wx.requestPayment({
                  timeStamp: wxconfig.timeStamp,
                  nonceStr: wxconfig.nonceStr,
                  package: wxconfig.package,
                  signType: wxconfig.signType,
                  paySign: wxconfig.paySign,
                  success() {
                    that.setData({
                        buyPop:true
                    })
                  },
                  fail() {
                    that.setData({
                        buyPop1:true
                    })
                  }
                })
              }else{
                that.setData({
                    buyPop:true
                })
              }
           }
          
           
              wx.hideLoading();
      
          }).catch((err) => {
            console.error(err);
          });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

})